package com.example.myapplication

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

class MainActivity: AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 创建一个垂直方向的线性布局
        val linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.gravity = Gravity.CENTER // 设置布局居中对齐

        // 创建一个ImageView并设置其属性（放国旗图片）
        val imageView = ImageView(this)
        imageView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        imageView.setImageResource(R.drawable.flag)
        linearLayout.addView(imageView)

        // 创建一个TextView并设置其属性
        val textView = TextView(this)
        textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.textSize=30F   //设置文本大小
        textView.text=getString(R.string.TextView)//设置文本内容
        linearLayout.addView(textView)  // 把textview加入到线性布局中


        val button =Button(this)
        // 设置按钮的背景颜色
        button.setBackgroundColor(ContextCompat.getColor(this,R.color.purple_500))
        // 设置按钮文本颜色
        button.setTextColor(ContextCompat.getColor(this, R.color.white))
        // 设置按钮的大小
        button.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        button.text=getString(R.string.button)
        linearLayout.addView(button)

//      设置组件之间的距离
        val marginLayoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        marginLayoutParams.setMargins(0, -10, 0, 100) // 设置间距
        textView.layoutParams = marginLayoutParams
        button.layoutParams = marginLayoutParams

        setContentView(linearLayout)

        button.setOnClickListener {
            textView.text = getString(R.string.clicked)
        }
    }
}